/*
 * @(#)CompanyInfoServiceImpl.java 2011-11-08
 *
 * Copyright 2011 北龙中网（北京）科技有限责任公司. All rights reserved.
 */
package cn.shangjia.shop.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.shangjia.common.domain.CompanyBasicOnline;
import cn.shangjia.common.domain.CompanyContactOnline;
import cn.shangjia.common.domain.CompanyDetailOnline;
import cn.shangjia.common.domain.CompanyInfo;
import cn.shangjia.common.exceptions.Logger;
import cn.shangjia.common.util.BeanUtils;
import cn.shangjia.common.util.StringUtils;
import cn.shangjia.shop.config.Constants;
import cn.shangjia.shop.dao.CompanyBasicOnlineDao;
import cn.shangjia.shop.dao.CompanyContactOnlineDao;
import cn.shangjia.shop.dao.CompanyDetailOnlineDao;
import cn.shangjia.shop.exception.NotFoundException;
import cn.shangjia.shop.exception.UserLockedException;
import cn.shangjia.shop.log.ShopLoggerFactory;
import cn.shangjia.shop.service.CompanyInfoService;

/**
 * 企业信息服务实现类.
 *
 * @author <a href="mailto:wangxin@knet.cn">wangxin</a>
 * @version 1.0, 2011-11-08 18:00:43
 * @since 1.0
 * 
 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
 */
@Service
public class CompanyInfoServiceImpl implements CompanyInfoService {
	
	private static final Logger logger = ShopLoggerFactory.getLogger(CompanyInfoService.class);
	
	@Resource
	private CompanyBasicOnlineDao companyBasicOnlineDao;
	
	@Resource
	private CompanyContactOnlineDao companyContactOnlineDao;
	
	@Resource
	private CompanyDetailOnlineDao companyDetailOnlineDao;
	

	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.CompanyInfoService#getCompanyInfoByUserId(java.lang.String)
	 * 
	 * jinzhognze 添加容错 和 日志
	 */
	@Override
	public CompanyInfo getCompanyInfoByUserId(String userId) {
		
		if (StringUtils.isEmpty(userId)) { //如果 用户ID不存在 则页面就不存在
			throw new NotFoundException();
		}
		CompanyBasicOnline basic = companyBasicOnlineDao.read(userId);
		if (basic == null) {  //如果 用户ID不存在 则页面就不存在
			logger.error("用户[" , userId , "]没有 企业基本信息数据, 检查是否有脏数据");
			throw new UserLockedException("用户[" + userId + "]被删除或锁定");
		}
		
		// 剃掉GBT -
		gbtConverter(basic);
		homePageConverter(basic);
		
		CompanyInfo companyInfo = new CompanyInfo();
		companyInfo.setCompanyBasicOnline(basic);
		
		CompanyContactOnline contact = companyContactOnlineDao.read(userId);
		if (contact == null) {
			logger.warn("用户[" , userId , "]没有 企业联系人信息数据, 检查是否有脏数据");
			contact = new CompanyContactOnline();
			BeanUtils.copyProperties(contact, basic);
		}
		companyInfo.setCompanyContactOnline(contact);
		
		CompanyDetailOnline detail = companyDetailOnlineDao.read(userId);
		if (detail == null) {
			logger.warn("用户[" , userId , "]没有 企业详细信息数据, 检查是否有脏数据");
			detail = new CompanyDetailOnline();
			BeanUtils.copyProperties(detail, basic);
		}
		companyInfo.setCompanyDetailOnline(detail);
		
		return companyInfo;
	}
	
	private void gbtConverter(CompanyBasicOnline basic) {
		if (basic == null )
			return;
		String gbt = basic.getGbt();
		if (StringUtils.isEmpty(gbt))
			return ;
		String[] gbts = gbt.split(",");
		for (String key  : gbts) {
			if (StringUtils.isEmpty(key))
				continue;
			String value = Constants.GBT_MAP.get(key);
			if (StringUtils.isEmpty(value))
				continue;
			gbt = gbt.replaceAll(key, value);
		}
		basic.setGbt(gbt);
	}
	
	private void homePageConverter(CompanyBasicOnline basic) {
		if (basic == null) 
			return ;
		String homePage = basic.getHomePage();
		if (StringUtils.isEmpty(homePage)) 
			return ;
		
		if (homePage.startsWith("http://"))
			return ;
		
		basic.setHomePage("http://" + homePage);
	}
}
